const app = getApp()
var util = require('../../utils/util.js')
Page({
  data: {
   update:'',
    afttowDate: '',
    basic:{},
    today:{},
    tomorrow:{},
    afttow:{},
    afterTomor:{},
    four:{},
    five:{},
    six:{},
    todyIcon:'',
    tomorrowIcon:'',
    afterTomorIcon:''
  },
  onShow: function () {
    this.getLocation();
  },
  onLoad: function () {
    this.setData({
      one: util.getDateStr(new Date(),0),
      two: util.getDateStr(new Date(),1),
      three: util.getDateStr(new Date(),2),
      fo: util.getDateStr(new Date(),3),
      fi: util.getDateStr(new Date(),4),
      si: util.getDateStr(new Date(),5),
      se: util.getDateStr(new Date(),6),
    });
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  
  getLocation:function(){
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        that.getWeatherInfo(latitude, longitude);
      }
    })
  },
  getWeatherInfo: function (latitude, longitude){
    var _this = this;
    var key = '82be48ac87064e978641b9c23819f4fb';//你自己的key
    //需要在微信公众号的设置-开发设置中配置服务器域名
    var url = 'https://free-api.heweather.com/s6/weather?key='+key+'&location=' + longitude + ',' + latitude;
    wx.request({
      url: url, 
      data: {},
      method: 'GET',
      success: function (res) {
        console.log(res);
        var daily_forecast_today = res.data.HeWeather6[0].daily_forecast[0];//今天预报
        var daily_forecast_tomorrow = res.data.HeWeather6[0].daily_forecast[1];//明天天预报
        var daily_forecast_afterTomor = res.data.HeWeather6[0].daily_forecast[2];//后天预报
        var daily_forecast_towafter = res.data.HeWeather6[0].daily_forecast[3];//后天预报+1
        var daily_forecast_four = res.data.HeWeather6[0].daily_forecast[4];//后天预报+2
        var daily_forecast_five = res.data.HeWeather6[0].daily_forecast[5];//后天预报+3
        var daily_forecast_six = res.data.HeWeather6[0].daily_forecast[6];//后天预报+4
        var basic = res.data.HeWeather6[0].basic;
        var update = res.data.HeWeather6[0].update.loc;//更新时间
        _this.setData({
          update:update,
          basic:basic,
          today: daily_forecast_today,
          tomorrow:daily_forecast_tomorrow,
          afterTomor: daily_forecast_afterTomor,
          afttow: daily_forecast_towafter,
           four:daily_forecast_four,
           five:daily_forecast_five,
           six:daily_forecast_six,
          todyIcon: '../imgs/' + daily_forecast_today.cond_code_d+'.png', //在和风天气中下载天气的icon图标，根据cond_code_d显示相应的图标
          tomorrowIcon: '../imgs/' + daily_forecast_tomorrow.cond_code_d+'.png',
          afterTomorIcon: '../imgs/' + daily_forecast_afterTomor.cond_code_d+'.png'
        });
      }
    })
  }
})
